package com.lhd.qd.module.system.dept.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhd.qd.common.base.ApiResult;
import com.lhd.qd.common.base.BaseController;
import com.lhd.qd.common.base.BasePageQuery;
import com.lhd.qd.module.system.dept.model.dto.DeptSaveDTO;
import com.lhd.qd.module.system.dept.model.vo.DeptDetailVO;
import com.lhd.qd.module.system.dept.model.vo.DeptListVO;
import com.lhd.qd.module.system.dept.model.vo.DeptTreeVO;
import com.lhd.qd.module.system.dept.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "科室")
@RequestMapping("dept")
@RestController
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @ApiOperation(value = "分页列表", response = DeptListVO.class)
    @GetMapping
    public ResponseEntity<ApiResult<IPage<DeptListVO>>> pageDept(BasePageQuery query) {

        return success(deptService.pageDept(query));
    }

    @ApiOperation(value = "获得医院下的科室树", response = DeptTreeVO.class)
    @GetMapping("tree/hospital/{hospitalId}")
    public ResponseEntity<ApiResult<List<DeptTreeVO>>> getDeptTreeByHospitalId(@PathVariable("hospitalId") Long hospitalId) {

        return success(deptService.getDeptTreeByHospitalId(hospitalId));
    }

    @ApiOperation(value = "详情", response = DeptDetailVO.class)
    @GetMapping("{id}")
    public ResponseEntity<ApiResult<DeptDetailVO>> getDeptById(@PathVariable("id") Long id) {

        return success(deptService.getDeptById(id));
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public ResponseEntity<ApiResult> saveDept(@Validated @RequestBody DeptSaveDTO saveDTO) {

        return successOrFail(deptService.saveDept(saveDTO));
    }

    @ApiOperation(value = "修改")
    @PutMapping("{id}")
    public ResponseEntity<ApiResult> updateDeptById(@PathVariable("id") Long id, @Validated @RequestBody DeptSaveDTO saveDTO) {

        return successOrFail(deptService.updateDeptById(id, saveDTO));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("{id}")
    public ResponseEntity<ApiResult> removeDeptById(@PathVariable("id") Long id) {

        return successOrFail(deptService.removeDeptById(id));
    }
}
